/**
 * 
 */

// Call onDeviceReady when PhoneGap is loaded.
//
// At this point, the document has loaded but phonegap-1.0.0.js has not.
// When PhoneGap is loaded and talking with the native device,
// it will call the event `deviceready`.
// 
function onLoad() {
	document.addEventListener("deviceready", onDeviceReady, false);
}

// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
//
function onDeviceReady() {
	// Register the event listener
	document.addEventListener("menubutton", onMenuKeyDown, false);
	document.addEventListener("backbutton", onBackKeyDown, false);
}

// Handle the back button
//
function onBackKeyDown() {
	// alert("Back")
	//$('#popupDialog').popup("open");
	if(confirm("Do you want to exit?")) {
		navigator.app.exitApp();
	}
	//navigator.app.exitApp();
}

// Handle the menu button
//
function onMenuKeyDown() {
	// alert("menu")
}

var data = {
	"entry" : [ {
		"id" : 1,
		"table" : "Bảng A",
		"day" : "08/06/2012",
		"time" : "23:00",
		"stadium" : "National Stadium, Warsaw",
		"team1" : "Ba Lan",
		"flag1" : "flags/Poland flag.png",
		"team2" : "Hy Lạp",
		"flag2" : "flags/Greece flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 2,
		"table" : "Bảng A",
		"day" : "09/06/2012",
		"time" : "1:45",
		"stadium" : "Municipal Stadium, Wrocław",
		"team1" : "Nga",
		"flag1" : "flags/Russia flag.png",
		"team2" : "CH Séc",
		"flag2" : "flags/Czech Republic flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 3,
		"table" : "Bảng B",
		"day" : "09/06/2012",
		"time" : "23:00",
		"stadium" : "Metalist Stadium, Kharkiv",
		"team1" : "Hà Lan",
		"flag1" : "flags/Nederland flag.png",
		"team2" : "Đan Mạch",
		"flag2" : "flags/Denmark flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 4,
		"table" : "Bảng B",
		"day" : "10/06/2012",
		"time" : "1:45",
		"stadium" : "Arena Lviv, Lviv",
		"team1" : "Đức",
		"flag1" : "flags/Germany flag.png",
		"team2" : "Bồ Đào Nha",
		"flag2" : "flags/Portugal flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 5,
		"table" : "Bảng C",
		"day" : "10/06/2012",
		"time" : "23:00",
		"stadium" : "PGE Arena, Gdańsk",
		"team1" : "Tây Ban Nha",
		"flag1" : "flags/Spain flag.png",
		"team2" : "Italia",
		"flag2" : "flags/Italy flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 6,
		"table" : "Bảng C",
		"day" : "11/06/2012",
		"time" : "1:45",
		"stadium" : "Municipal Stadium, Poznań",
		"team1" : "Ireland",
		"flag1" : "flags/Ireland flag.png",
		"team2" : "Croatia",
		"flag2" : "flags/Croatia flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 7,
		"table" : "Bảng D",
		"day" : "11/06/2012",
		"time" : "23:00",
		"stadium" : "Donbass Arena, Donetsk",
		"team1" : "Pháp",
		"flag1" : "flags/France flag.png",
		"team2" : "Anh",
		"flag2" : "flags/England flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 8,
		"table" : "Bảng D",
		"day" : "12/06/2012",
		"time" : "1:45",
		"stadium" : "Olympic Stadium, Kiev",
		"team1" : "Ukraine",
		"flag1" : "flags/Ukraine flag.png",
		"team2" : "Thụy Điển",
		"flag2" : "flags/Sweden flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 9,
		"table" : "Bảng A",
		"day" : "12/06/2012",
		"time" : "23:00",
		"stadium" : "Municipal Stadium, Wrocław",
		"team1" : "Hy Lạp",
		"flag1" : "flags/Greece flag.png",
		"team2" : "CH Séc",
		"flag2" : "flags/Czech Republic flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 10,
		"table" : "Bảng A",
		"day" : "13/06/2012",
		"time" : "1:45",
		"stadium" : "National Stadium, Warsaw",
		"team1" : "Ba Lan",
		"flag1" : "flags/Poland flag.png",
		"team2" : "Nga",
		"flag2" : "flags/Russia flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 11,
		"table" : "Bảng B",
		"day" : "13/06/2012",
		"time" : "23:00",
		"stadium" : "Arena Lviv, Lviv",
		"team1" : "Đan Mạch",
		"flag1" : "flags/Denmark flag.png",
		"team2" : "Bồ Đào Nha",
		"flag2" : "flags/Portugal flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 12,
		"table" : "Bảng B",
		"day" : "14/06/2012",
		"time" : "1:45",
		"stadium" : "Metalist Stadium, Kharkiv",
		"team1" : "Hà Lan",
		"flag1" : "flags/Nederland flag.png",
		"team2" : "Đức",
		"flag2" : "flags/Germany flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 13,
		"table" : "Bảng C",
		"day" : "14/06/2012",
		"time" : "23:00",
		"stadium" : "Municipal Stadium, Poznań",
		"team1" : "Italia",
		"flag1" : "flags/Italy flag.png",
		"team2" : "Croatia",
		"flag2" : "flags/Croatia flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 14,
		"table" : "Bảng C",
		"day" : "15/06/2012",
		"time" : "1:45",
		"stadium" : "PGE Arena, Gdańsk",
		"team1" : "Tây Ban Nha",
		"flag1" : "flags/Spain flag.png",
		"team2" : "Ireland",
		"flag2" : "flags/Ireland flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 15,
		"table" : "Bảng D",
		"day" : "15/06/2012",
		"time" : "23:00",
		"stadium" : "Donbass Arena, Donetsk",
		"team1" : "Thụy Điển",
		"flag1" : "flags/Sweden flag.png",
		"team2" : "Anh",
		"flag2" : "flags/England flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 16,
		"table" : "Bảng D",
		"day" : "16/06/2012",
		"time" : "1:45",
		"stadium" : "Olympic Stadium, Kiev",
		"team1" : "Ukraine",
		"flag1" : "flags/Ukraine flag.png",
		"team2" : "Pháp",
		"flag2" : "flags/France flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 17,
		"table" : "Bảng A",
		"day" : "17/06/2012",
		"time" : "1:45",
		"stadium" : "Municipal Stadium, Wrocław",
		"team1" : "CH Séc",
		"flag1" : "flags/Czech Republic flag.png",
		"team2" : "Ba Lan",
		"flag2" : "flags/Russia flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 18,
		"table" : "Bảng A",
		"day" : "17/06/2012",
		"time" : "1:45",
		"stadium" : "National Stadium, Warsaw",
		"team1" : "Hy Lạp",
		"flag1" : "flags/Greece flag.png",
		"team2" : "Nga",
		"flag2" : "flags/Russia flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 19,
		"table" : "Bảng B",
		"day" : "18/06/2012",
		"time" : "1:45",
		"stadium" : "Metalist Stadium, Kharkiv",
		"team1" : "Bồ Đào Nha",
		"flag1" : "flags/Portugal flag.png",
		"team2" : "Hà Lan",
		"flag2" : "flags/Nederland flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 20,
		"table" : "Bảng B",
		"day" : "18/06/2012",
		"time" : "1:45",
		"stadium" : "Arena Lviv, Lviv",
		"team1" : "Đan Mạch",
		"flag1" : "flags/Denmark flag.png",
		"team2" : "Đức",
		"flag2" : "flags/Germany flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 21,
		"table" : "Bảng C",
		"day" : "19/06/2012",
		"time" : "1:45",
		"stadium" : "PGE Arena, Gdańsk",
		"team1" : "Croatia",
		"flag1" : "flags/Croatia flag.png",
		"team2" : "Tây Ban Nha",
		"flag2" : "flags/Spain flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 22,
		"table" : "Bảng C",
		"day" : "19/06/2012",
		"time" : "1:45",
		"stadium" : "Municipal Stadium, Poznań",
		"team1" : "Italia",
		"flag1" : "flags/Italy flag.png",
		"team2" : "Ireland",
		"flag2" : "flags/Ireland flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 23,
		"table" : "Bảng D",
		"day" : "20/06/2012",
		"time" : "1:45",
		"stadium" : "Donbass Arena, Donetsk",
		"team1" : "Anh",
		"flag1" : "flags/England flag.png",
		"team2" : "Ukraine",
		"flag2" : "flags/Ukraine flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 24,
		"table" : "Bảng D",
		"day" : "20/06/2012",
		"time" : "1:45",
		"stadium" : "Olympic Stadium, Kiev",
		"team1" : "Thụy Điển",
		"flag1" : "flags/Sweden flag.png",
		"team2" : "Pháp",
		"flag2" : "flags/France flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 25,
		"table" : "Tứ kết 1",
		"day" : "22/06/2012",
		"time" : "1:45",
		"stadium" : "National Stadium, Warsaw",
		"team1" : "Nhất A",
		"flag1" : "flags/flag.png",
		"team2" : "Nhì B",
		"flag2" : "flags/flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 26,
		"table" : "Tứ kết 2",
		"day" : "23/06/2012",
		"time" : "1:45",
		"stadium" : "PGE Arena, Gdańsk",
		"team1" : "Nhất B",
		"flag1" : "flags/flag.png",
		"team2" : "Nhì A",
		"flag2" : "flags/flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 27,
		"table" : "Tứ kết 3",
		"day" : "24/06/2012",
		"time" : "1:45",
		"stadium" : "Donbass Arena, Donetsk",
		"team1" : "Nhất C",
		"flag1" : "flags/flag.png",
		"team2" : "Nhì D",
		"flag2" : "flags/flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 28,
		"table" : "Tứ kết 4",
		"day" : "25/06/2012",
		"time" : "1:45",
		"stadium" : "Olympic Stadium, Kiev",
		"team1" : "Nhất D",
		"flag1" : "flags/flag.png",
		"team2" : "Nhì C",
		"flag2" : "flags/flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 29,
		"table" : "Bán kết 1",
		"day" : "28/06/2012",
		"time" : "1:45",
		"stadium" : "Donbass Arena, Donetsk",
		"team1" : "Thắng TK1",
		"flag1" : "flags/flag.png",
		"team2" : "Thắng TK3",
		"flag2" : "flags/flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 30,
		"table" : "Bán kết 2",
		"day" : "29/06/2012",
		"time" : "1:45",
		"stadium" : "National Stadium, Warsaw",
		"team1" : "Thắng TK2",
		"flag1" : "flags/flag.png",
		"team2" : "Thắng TK4",
		"flag2" : "flags/flag.png",
		"mark1" : "-",
		"mark2" : "-"
	}, {
		"id" : 31,
		"table" : "Trung kết",
		"day" : "02/07/2012",
		"time" : "1:45",
		"stadium" : "Olympic Stadium, Kiev",
		"team1" : "Thắng BK1",
		"flag1" : "flags/flag.png",
		"team2" : "Thắng BK2",
		"flag2" : "flags/flag.png",
		"mark1" : "-",
		"mark2" : "-"
	} ]
};
flags = new Array();
country= new Array();
init();
function init() {
	flags["Ba Lan"] = "Poland-flag.png";
	flags["Croatia"] = "Croatia-flag.png";
	flags["CH Séc"] = "Czech-Republic-flag.png";
	flags["Đan Mạch"] = "Denmark-flag.png";

	flags["Anh"] = "England-flag.png";
	flags["Pháp"] = "France-flag.png";
	flags["Đức"] = "Germany-flag.png";
	flags["Hy Lạp"] = "Greece-flag.png";

	flags["Ireland"] = "Ireland-flag.png";
	flags["Italia"] = "Italy-flag.png";
	flags["Hà Lan"] = "Nederland-flag.png";
	flags["Bồ Đào Nha"] = "Portugal-flag.png";

	flags["Nga"] = "Russia-flag.png";
	flags["Tây Ban Nha"] = "Spain-flag.png";
	flags["Thụy Điển"] = "Sweden-flag.png";
	flags["Ukraine"] = "Ukraine-flag.png";
	
	country["Russia"]="Nga";
	country["Greece"]="Hy Lạp";
	country["Poland"]="Ba Lan";
	country["Czech Republic"]="CH Séc";

	country["Germany"]="Đức";
	country["Denmark"]="Đan Mạch";
	country["Holland"]="Hà Lan";
	country["Portugal"]="Bồ Đào Nha";

	country["Croatia"]="Croatia";
	country["Spain"]="Tây Ban Nha";
	country["Italy"]="Italia";
	country["Rep. of Ireland"]="Ireland";

	country["England"]="Anh";
	country["Sweden"]="Thụy Điển";
	country["France"]="Pháp";
	country["Ukraine"]="Ukraine";
}
$(function() {
	setTimeout(hideSplash, 4100);
});

function hideSplash() {
	$.mobile.changePage("#home", "none");
}
$(document)
		.ready(
				function() {
					//alert("ready");
					
					$('.my-collaspible').bind('expand', function () {
					    $(this).children().slideDown(200);
					}).bind('collapse', function () {
					    $(this).children().next().slideUp(200);
					});
					$("#menuChoice").find("input[type='radio']").each(function(){
						$(this).change(function(){
							init();
							//alert("radio changed"+this.id); 
							if(this.id=="radio-choice-1"){
								$("#listMatch").show();	
								$("#knockout").hide();
								$("#listGroup").hide();
								if(!loaded) {
									loadOffline();
									getAllMatch();
									loaded=true;
								}								
							}
							if(this.id=="radio-choice-2"){
								$("#listMatch").hide();
								$("#knockout").hide();
								$("#listGroup").show();
								if(!loadGroup) {
									loadListGroup();
									getAllMatch();
									loadGroup=true;
									
								}
							}
							if(this.id=="radio-choice-3"){
								$("#listMatch").hide();
								$("#knockout").show();
								$("#listGroup").hide();
							}
							});  
						});
				}
				
		);
$(window).load(function() {
	//alert("load");	
	var loaded=false;
	getAllMatch();
	getPointGroupA();
	getPointGroupB();
	getPointGroupC();
	getPointGroupD();
	parseRSS("http://bongdaplus.vn/_RSS_/4.rss", displaySkiReport);
	$("#knockout").hide();
	$("#listMatch").hide();
	$("#listGroup").hide();
	loadCountDown();
	loadOffline();
});
function gotoPage(page) {
	//$.mobile.changePage(page, "none");
	if(page!="undefined.html") {
		$(location).attr('href',page);
		//navigator.app.loadUrl(page);
	}
}
function gotoHome() {
	$(location).attr('href','#home');
}
var loaded=false;
var loadGroup=false;
function loadOffline() {
	mackup = "";
	pre="";
	start=true;
	end="";
	inner="";
	ht="";
		
		
	$('#listMatch').empty();
	for ( var i = 0; i < data.entry.length;) {
		item = data.entry[i];
		
		pre = '<div data-role="collapsible" data-inset="true" data-iconpos="right" >'
				+ '<h3 style="background-color:#ededed">'+item.day+'</h3>';
		pretime=item.day;
		current=item.day;
		
		while(pretime==current) {
			
			inner+='<div style="background-color:#fff;">'
						+ '<p style="margin: 0px;padding-bottom:1.2em;padding-top:0.5em">'
							+ '<span class="ui-hours" style="font-weight:normal;"><i>'+ item.time+' <span>'+ item.table+'</span>'+ '</i> </span><span class="ui-li-aside ui-stadium" style="width:70%;font-weight:normal;font-size:0.7em;margin: auto">'
							+ item.stadium 
							+ '</span>'
						+ '</p>'
							+ '<div class="ui-grid-b" style="clear:left;border-bottom:1px solid #ddd;">'
								+ '<div class="ui-block-a">'
									+ '<div style="height: 4em"  align="center" ><span id="team1_'
									+ item.id
									+ '" align="center" style="font-size: 1em;text-shadow: white 0px 1px 0px;">'
									+'<img src="flags/'
									+ flags[item.team1]
									+ '" class="flag"  onclick="gotoPage('+"'#"+flags[item.team1]+"'"+')" /><br/><span style="clear:left">'
									+ item.team1
									+ '</span> </span></div>'
							+ '</div>'
							+ '<div class="ui-block-b" >'
							+'<div>&nbsp;</div>'
								+ '<div style="height: 3.5em">'
								+'<span id="match_'
								+ item.id
								+ '_1" class="ui-circle">'
								+ item.mark1
								+ '</span> <span>-</span> <span id="match_'
								+ item.id
								+ '_2" class="ui-circle">'
								+ item.mark2
								+ '</span>'
								+ '</div>'
							+ '</div>'
							+ '<div class="ui-block-c">'
								+ '<div style="height: 4em"  align="center" >'
								+ '<span id="team2_'
								+ item.id
								+ '" align="center" style="font-size: 1em;text-shadow: white 0px 1px 0px;"><img src="flags/'
								+ flags[item.team2]
								+ '" class="flag" onclick="gotoPage('+"'#"+flags[item.team2]+"'"+')" /><br/><span style="clear:left"> '
								+ item.team2
								+ '</span> </span>'
								+ '</div>'
							+'</div>'
				+ '</div>' ;
			i++;
			if(i>=data.entry.length) {
				break;
			}
			item = data.entry[i];
			pretime=current;
			current=item.day;
		}
		end="</div>";
		ht=pre+inner+end;
		inner="";
		$("#listMatch").append(ht);
	}
	
	$("#listMatch").trigger('create');
	
}
function loadListGroup(){
	$('#listGroup').empty();
	var groupA='<div data-role="collapsible" data-inset="true" data-iconpos="right" >'
				+ '<h3 style="background-color:#ededed">Bảng A</h3>';
	var groupB='<div data-role="collapsible" data-inset="true" data-iconpos="right" >'
				+ '<h3 style="background-color:#ededed">Bảng B</h3>';
	var groupC='<div data-role="collapsible" data-inset="true" data-iconpos="right" >'
				+ '<h3 style="background-color:#ededed">Bảng C</h3>';
	var groupD='<div data-role="collapsible" data-inset="true" data-iconpos="right" >'
				+ '<h3 style="background-color:#ededed">Bảng D</h3>';
	var tmp="";
	for ( var i = 0; i < data.entry.length;i++) {
		item = data.entry[i];
		tmp='<div style="background-color:#fff;">'
			+ '<p style="margin: 0px;padding-bottom:1.2em;padding-top:0.5em">'
			+ '<span class="ui-hours" style="font-weight:normal;"><i>'+ item.day+'<br/>'+ item.time+ '</i> </span><span class="ui-li-aside ui-stadium" style="width:70%;font-weight:normal;font-size:0.7em;margin: auto">'+ item.stadium 
			+ '</span>'
		+ '</p>'
			+ '<div class="ui-grid-b" style="clear:left;border-bottom:1px solid #ddd;">'
				+ '<div class="ui-block-a">'
					+ '<div style="height: 4em"  align="center" ><span id="team1_'
					+ item.id
					+ '" align="center" style="font-size: 1em;text-shadow: white 0px 1px 0px;">'
					+'<img src="flags/'
					+ flags[item.team1]
					+ '" class="flag"  onclick="gotoPage('+"'#"+flags[item.team1]+"'"+')" /><br/><span style="clear:left">'
					+ item.team1
					+ '</span> </span></div>'
			+ '</div>'
			+ '<div class="ui-block-b" >'
			+'<div>&nbsp;</div>'
				+ '<div style="height: 3.5em">'
				+'<span id="match_'
				+ item.id
				+ '_1_" class="ui-circle">'
				+ item.mark1
				+ '</span> <span>-</span> <span id="match_'
				+ item.id
				+ '_2_" class="ui-circle">'
				+ item.mark2
				+ '</span>'
				+ '</div>'
			+ '</div>'
			+ '<div class="ui-block-c">'
				+ '<div style="height: 4em"  align="center" >'
				+ '<span id="team2_'
				+ item.id
				+ '" align="center" style="font-size: 1em;text-shadow: white 0px 1px 0px;"><img src="flags/'
				+ flags[item.team2]
				+ '" class="flag" onclick="gotoPage('+"'#"+flags[item.team2]+"'"+')" /><br/><span style="clear:left"> '
				+ item.team2
				+ '</span> </span>'
				+ '</div>'
			+'</div>'
+ '</div>' ;
		if(item.table=='Bảng A'){
			groupA+=tmp;;
		}
		if(item.table=='Bảng B'){
			groupB+=tmp;;
		}
		if(item.table=='Bảng C'){
			groupC+=tmp;;
		}
		if(item.table=='Bảng D'){
			groupD+=tmp;;
		}
	}
	groupA+='</div>';
	$("#listGroup").append(groupA);
	groupB+='</div>';
	$("#listGroup").append(groupB);
	groupC+='</div>';
	$("#listGroup").append(groupC);
	groupD+='</div>';
	$("#listGroup").append(groupD);
	$("#listGroup").trigger('create');
}
function loadCountDown() {
	var austDay = new Date();
	austDay = new Date(2012, 6 - 1, 8);	
	$("#countdown").remove();
	$('#listMatchToday').append('<div align="center" id="countdown"></div>');
	$('#countdown').countdown({
		until : austDay
	});
}
calendar = {
	"entry" : []
};
groupA = {
	"entry" : []
};
groupB = {
	"entry" : []
};
groupC = {
	"entry" : []
};
groupD = {
	"entry" : []
};


function getAllMatch() {
	// jQuery.noConflict();
	jQuery
			.getJSON(
					"https://spreadsheets.google.com/feeds/cells/0Ai_ehSqeQXC8dEh6eGJNUUMzNFJZWmJoS0NLNTRZLVE/1/public/basic?alt=json",
					getAllCallback);
}

function getAllCallback(json) {
	TOTAL_COLUMN = 11;
	var rows = json.feed.entry.length / TOTAL_COLUMN + 1;
	match = {};
	var j = 0;
	for ( var i = 2; i < rows; i++) {
		match = {};
		match.id = getbyrowandcolum(json, i, 1, TOTAL_COLUMN);
		match.day = getbyrowandcolum(json, i, 2, TOTAL_COLUMN);
		match.time = getbyrowandcolum(json, i, 3, TOTAL_COLUMN);
		match.group = getbyrowandcolum(json, i, 4, TOTAL_COLUMN);
		match.stadium = getbyrowandcolum(json, i, 5, TOTAL_COLUMN);
		match.team1 = getbyrowandcolum(json, i, 6, TOTAL_COLUMN);
		match.team2 = getbyrowandcolum(json, i, 7, TOTAL_COLUMN);
		match.mark1 = getbyrowandcolum(json, i, 8, TOTAL_COLUMN);
		match.mark2 = getbyrowandcolum(json, i, 9, TOTAL_COLUMN);
		calendar.entry[j++] = match;
	}
	updateToday(calendar);
	updateData(calendar);
	return calendar;
}

function updateData(calendar) {
	for ( var i = 0; i < calendar.entry.length; i++) {
		match = calendar.entry[i];
		$("#match_" + match.id + "_1").text(match.mark1);
		$("#match_" + match.id + "_2").text(match.mark2);
		$("#match_" + match.id + "_1_").text(match.mark1);
		$("#match_" + match.id + "_2_").text(match.mark2);
		$("#match_" + match.id + "_1__").text(match.mark1);
		$("#match_" + match.id + "_2__").text(match.mark2);
	}
	if (calendar.entry[24].team1 != '-') {
		$("#team1_25").empty();
		$("#team1_25").append(' <img src="flags/'
						+ flags[calendar.entry[24].team1]
						+ '" class="flag"><br/>'+calendar.entry[24].team1);
		$("#team2_25").empty();
		$("#team2_25").append( ' <img src="flags/'
						+ flags[calendar.entry[24].team2]
						+ '" class="flag"><br/>'+calendar.entry[24].team2);
		
		$("#TK1A").empty();
		$("#TK1A").append( '<img src="flags/'
						+ flags[calendar.entry[24].team1]
						+ '" class="flag"><br/>'+calendar.entry[24].team1);
		$("#TK1B").empty();
		$("#TK1B").append( ' <img src="flags/'
						+ flags[calendar.entry[24].team2]
						+ '" class="flag"><br/>'+calendar.entry[24].team2);
		$("#TK1goalA").text(calendar.entry[24].mark1);
		$("#TK1goalB").text(calendar.entry[24].mark2);
	}
	if (calendar.entry[25].team1 != '-') {
		$("#team1_26").empty();
		$("#team1_26").append(' <img src="flags/'
						+ flags[calendar.entry[25].team1]
						+ '" class="flag"><br/>'+calendar.entry[25].team1);
		$("#team2_26").empty();
		$("#team2_26").append( ' <img src="flags/'
						+ flags[calendar.entry[25].team2]
						+ '" class="flag"><br/>'+calendar.entry[25].team2);
		
		$("#TK2B").empty();
		$("#TK2B").append( ' <img src="flags/'
						+ flags[calendar.entry[25].team1]
						+ '" class="flag"><br/>'+calendar.entry[25].team1);
		$("#TK2A").empty();
		$("#TK2A").append( ' <img src="flags/'
						+ flags[calendar.entry[25].team2]
						+ '" class="flag"><br/>'+calendar.entry[25].team2);
		$("#TK2goalB").text(calendar.entry[25].mark1);
		$("#TK2goalA").text(calendar.entry[25].mark2);
	}
	if (calendar.entry[26].team1 != '-') {
		$("#team1_27").empty();
		$("#team1_27").append(' <img src="flags/'
						+ flags[calendar.entry[26].team1]
						+ '" class="flag"><br/>'+calendar.entry[26].team1);
		$("#team2_27").empty();
		$("#team2_27").append( ' <img src="flags/'
						+ flags[calendar.entry[26].team2]
						+ '" class="flag"><br/>'+calendar.entry[26].team2);
		
		$("#TK3C").empty();
		$("#TK3C").append( ' <img src="flags/'
						+ flags[calendar.entry[26].team1]
						+ '" class="flag"><br/>'+calendar.entry[26].team1);
		$("#TK3D").empty();
		$("#TK3D").append( ' <img src="flags/'
						+ flags[calendar.entry[26].team2]
						+ '" class="flag"><br/>'+calendar.entry[26].team2);
		$("#TK3goalC").text(calendar.entry[26].mark1);
		$("#TK3goalD").text(calendar.entry[26].mark2);
	}
	if (calendar.entry[27].team1 != '-') {
		$("#team1_28").empty();
		$("#team1_28").append(' <img src="flags/'
						+ flags[calendar.entry[27].team1]
						+ '" class="flag"><br/>'+calendar.entry[27].team1);
		$("#team2_28").empty();
		$("#team2_28").append( ' <img src="flags/'
						+ flags[calendar.entry[27].team2]
						+ '" class="flag"><br/>'+calendar.entry[27].team2);
		
		$("#TK4D").empty();
		$("#TK4D").append( ' <img src="flags/'
						+ flags[calendar.entry[27].team1]
						+ '" class="flag"><br/>'+calendar.entry[27].team1);
		$("#TK4C").empty();
		$("#TK4C").append( ' <img src="flags/'
						+ flags[calendar.entry[27].team2]
						+ '" class="flag"><br/>'+calendar.entry[27].team2);
		$("#TK4goalD").text(calendar.entry[27].mark1);
		$("#TK4goalC").text(calendar.entry[27].mark2);
	}
	if (calendar.entry[28].team1 != '-') {
		$("#team1_29").empty();
		$("#team1_29").append(' <img src="flags/'
						+ flags[calendar.entry[28].team1]
						+ '" class="flag"><br/>'+calendar.entry[28].team1);
		$("#team2_29").empty();
		$("#team2_29").append( ' <img src="flags/'
						+ flags[calendar.entry[28].team2]
						+ '" class="flag"><br/>'+calendar.entry[28].team2);
		
		$("#ThangTK1").empty();
		$("#ThangTK1").append( ' <img src="flags/'
						+ flags[calendar.entry[28].team1]
						+ '" class="flag"><br/>'+calendar.entry[28].team1);
		$("#ThangTK3").empty();
		$("#ThangTK3").append( ' <img src="flags/'
						+ flags[calendar.entry[28].team2]
						+ '" class="flag"><br/>'+calendar.entry[28].team2);
		$("#ThangTK1goal").text(calendar.entry[28].mark1);
		$("#ThangTK3goal").text(calendar.entry[28].mark2);
	}
	if (calendar.entry[29].team1 != '-') {
		$("#team1_30").empty();
		$("#team1_30").append(' <img src="flags/'
						+ flags[calendar.entry[29].team1]
						+ '" class="flag"><br/>'+calendar.entry[29].team1);
		$("#team2_30").empty();
		$("#team2_30").append( ' <img src="flags/'
						+ flags[calendar.entry[29].team2]
						+ '" class="flag"><br/>'+calendar.entry[29].team2);
		
		$("#ThangTK2").empty();
		$("#ThangTK2").append( ' <img src="flags/'
						+ flags[calendar.entry[29].team1]
						+ '" class="flag"><br/>'+calendar.entry[29].team1);
		$("#ThangTK4").empty();
		$("#ThangTK4").append( ' <img src="flags/'
						+ flags[calendar.entry[29].team2]
						+ '" class="flag"><br/>'+calendar.entry[29].team2);
		$("#ThangTK2goal").text(calendar.entry[29].mark1);
		$("#ThangTK4goal").text(calendar.entry[29].mark2);
	}
	if (calendar.entry[30].team1 != '-') {
		$("#team1_31").empty();
		$("#team1_31").append(' <img src="flags/'
						+ flags[calendar.entry[30].team1]
						+ '" class="flag"><br/>'+calendar.entry[30].team1);
		$("#team2_31").empty();
		$("#team2_31").append( ' <img src="flags/'
						+ flags[calendar.entry[30].team2]
						+ '" class="flag"><br/>'+calendar.entry[30].team2);
		
		$("#ThangBK1").empty();
		$("#ThangBK1").append( ' <img src="flags/'
						+ flags[calendar.entry[30].team1]
						+ '" class="flag"><br/>'+calendar.entry[30].team1);
		$("#ThangBK2").empty();
		$("#ThangBK2").append( ' <img src="flags/'
						+ flags[calendar.entry[30].team2]
						+ '" class="flag"><br/>'+calendar.entry[30].team2);
		$("#ThangBK1goal").text(calendar.entry[30].mark1);
		$("#ThangBK2goal").text(calendar.entry[30].mark2);
	}
}
function getTomorrow(d, offset) {
	if (!offset) {
		offset = 1;
	}
	return new Date(new Date().setDate(d.getDate() + offset));
}
function updateToday(calendar) {
	init();
	list = new Array();
	var today = new Date();
	var dd = today.getDate();
	var mm = today.getMonth() + 1;
	var yyyy = today.getFullYear();
	if (dd < 10) {
		dd = '0' + dd;
	}
	;
	if (mm < 10) {
		mm = '0' + mm;
	}
	;
	today = dd + '/' + mm + '/' + yyyy;

	// alert(calendar.entry[0].day==today)
	j = 0;
	for ( var i = 0; i < calendar.entry.length; i++) {
		match = calendar.entry[i];
		if (match.day == today) {
			list[j] = match;
			j++;
		}
	}

	mackup = "";
	var pretime="";
	for ( var i = 0; i < list.length; i++) {
		item = list[i];
		if(pretime!=item.day) {
			mackup += '<div data-inset="true" style="clear:right;background-color:#ededed;font-size: 0.8em;text-align:center">'+ item.day
				+ '</div>';
			pretime=item.day;
		}
				mackup+='<div style="background-color:#fff;">'
					+ '<p style="margin: 0px;padding-bottom:1.2em;padding-top:0.5em;padding-left: 1em;padding-right: 1em;">'
					+ '<span class="ui-hours" style="font-weight:normal;"><i>'+ item.time+' <span>'+ item.group+'</span>'+ '</i> </span><span class="ui-li-aside ui-stadium" style="font-weight:normal;font-size:0.7em;margin: auto">'
					+ item.stadium 
					+ '</span>'
				+ '</p>'
					+ '<div class="ui-grid-b" style="clear:left;border-bottom:1px solid #ddd;">'
						+ '<div class="ui-block-a">'
							+ '<div style="height: 4em"  align="center" ><span id="team1_'
							+ item.id
							+ '" align="center" style="font-size: 1em;font-weight:bold;text-shadow: white 0px 1px 0px;">'
							+'<img src="flags/'
							+ flags[item.team1]
							+ '" class="flag" onclick="gotoPage('+"'#"+flags[item.team1]+"'"+')" /><br/><span style="clear:left">'
							+ item.team1
							+ '</span> </span></div>'
					+ '</div>'
					+ '<div class="ui-block-b" >'
					+'<div>&nbsp;</div>'
						+ '<div style="height: 3.5em">'
						+'<span id="match_'
						+ item.id
						+ '_1__" class="ui-circle">'
						+ item.mark1
						+ '</span> <span>-</span> <span id="match_'
						+ item.id
						+ '_2__" class="ui-circle">'
						+ item.mark2
						+ '</span>'
						+ '</div>'
					+ '</div>'
					+ '<div class="ui-block-c">'
						+ '<div style="height: 4em"  align="center" >'
						+ '<span id="team2_'
						+ item.id
						+ '" align="center" style="font-size: 1em;font-weight:bold;text-shadow: white 0px 1px 0px;"><img src="flags/'
						+ flags[item.team2]
						+ '" class="flag" onclick="gotoPage('+"'#"+flags[item.team2]+"'"+')" /><br/><span style="clear:left"> '
						+ item.team2
						+ '</span> </span>'
						+ '</div>'
					+'</div>'
		+ '</div>' ;
	}
	
	
	if (list.length > 0) {		
		$("#countdown").hide();
		$("#lblToday").text("Hôm nay");	
		$("#listMatchToday").empty();
		$("#listMatchToday").append(mackup);
		$("#listMatchToday").trigger('create');
	} else {	
		loadCountDown();
	}
}
function getbyrowandcolum(json, row, col, totalcolum) {

	return json.feed.entry[col - 1 + (row - 1) * totalcolum].content.$t;
}
function getPointGroupA() {
	jQuery
			.getJSON(
					"https://spreadsheets.google.com/feeds/cells/0Ai_ehSqeQXC8dEh6eGJNUUMzNFJZWmJoS0NLNTRZLVE/2/public/basic?alt=json",
					getGroupACallback);
}
function getGroupACallback(json) {
	var grStr=getGroupCallback(json);
	$("#groupA").empty();
	$("#groupA").append(grStr);
}
function getPointGroupB() {
	jQuery
			.getJSON(
					"https://spreadsheets.google.com/feeds/cells/0Ai_ehSqeQXC8dEh6eGJNUUMzNFJZWmJoS0NLNTRZLVE/3/public/basic?alt=json",
					getGroupBCallback);
}
function getGroupBCallback(json) {
	var grStr=getGroupCallback(json);
	$("#groupB").empty();
	$("#groupB").append(grStr);
}
function getPointGroupC() {
	jQuery
			.getJSON(
					"https://spreadsheets.google.com/feeds/cells/0Ai_ehSqeQXC8dEh6eGJNUUMzNFJZWmJoS0NLNTRZLVE/4/public/basic?alt=json",
					getGroupCCallback);
}
function getGroupCCallback(json) {
	var grStr=getGroupCallback(json);
	$("#groupC").empty();
	$("#groupC").append(grStr);
}
function getPointGroupD() {
	jQuery
			.getJSON(
					"https://spreadsheets.google.com/feeds/cells/0Ai_ehSqeQXC8dEh6eGJNUUMzNFJZWmJoS0NLNTRZLVE/5/public/basic?alt=json",
					getGroupDCallback);
}
function getGroupDCallback(json) {
	var grStr=getGroupCallback(json);
	$("#groupD").empty();
	$("#groupD").append(grStr);
}
function getGroupCallback(json) {
	group = {
		"entry" : []
	};	
	TOTAL_COLUMN = 9;
	var rows = json.feed.entry.length / TOTAL_COLUMN + 1;
	var j = 0;
	for ( var i = 2; i < rows; i++) {
		match = {};
		match.team = getbyrowandcolum(json, i, 1, TOTAL_COLUMN);
		match.played = getbyrowandcolum(json, i, 2, TOTAL_COLUMN);
		match.won = getbyrowandcolum(json, i, 3, TOTAL_COLUMN);
		match.drawn = getbyrowandcolum(json, i, 4, TOTAL_COLUMN);
		match.lost = getbyrowandcolum(json, i, 5, TOTAL_COLUMN);
		match.goalsFor = getbyrowandcolum(json, i, 6, TOTAL_COLUMN);
		match.goalsAgainst = getbyrowandcolum(json, i, 7, TOTAL_COLUMN);
		match.goalDifference = getbyrowandcolum(json, i, 8, TOTAL_COLUMN);
		match.points = getbyrowandcolum(json, i, 9, TOTAL_COLUMN);
		group.entry[j++] = match;
	}
	return updateGroup(group);
}
function updateGroup(group) {
	var groupStr='<table class="wikitable" style="text-align: center">'+
						'<tr>'+
									'<th width="165">Team</th>'+
									'<th width="20"><abbr title="Played">Pld</abbr></th>'+
									'<th width="20"><abbr title="Won">W</abbr></th>'+
									'<th width="20"><abbr title="Drawn">D</abbr></th>'+
									'<th width="20"><abbr title="Lost">L</abbr></th>'+
									'<th width="20"><abbr title="Goals for">GF</abbr></th>'+
									'<th width="20"><abbr title="Goals against">GA</abbr></th>'+
									'<th width="20"><abbr title="Goal difference">GD</abbr></th>'+
									'<th width="20"><abbr title="Points">Pts</abbr></th>'+
								'</tr>'
	for ( var i = 0; i < group.entry.length; i++) {
		var item=group.entry[i];
		groupStr+='<tr><td align="left"><span class="flagicon">';
		var vietnameseStr=country[item.team]
		groupStr+='<img alt=""src="flags/'+flags[vietnameseStr]+'" width="22" height="22"class="thumbborder">&nbsp;</span>'+vietnameseStr+'</td>'+
									'<td>'+item.played+'</td>'+
									'<td>'+item.won+'</td>'+
									'<td>'+item.drawn+'</td>'+
									'<td>'+item.lost+'</td>'+
									'<td>'+item.goalsFor+'</td>'+
									'<td>'+item.goalsAgainst+'</td>'+
									'<td>'+item.goalDifference+'</td>'+
									'<td><b>'+item.points+'</b></td>'+
								'</tr>'			
	}
	groupStr+='</tbody></table>'
	return groupStr;
}

function displaySkiReport(feedResponse) {
	mackup = "";
	for ( var i = 0; i < feedResponse.entries.length; i++) {
		item = feedResponse.entries[i];
		mackup += '<li data-theme="c"><a href="' + item.link
				+ '" data-transition="slide">';
		mackup += '<p style="font-weight:bold;font-size:1.1em;white-space:pre-wrap;text-align:justify;">'
				+ item.title + '</p>';
		mackup += '<span><p style="white-space:pre-line;text-align:justify;">'
				+ item.content + '</p></span>';
		mackup += '</a></li>';
	}
	$('#listNews').empty();
	$("#listNews").append(mackup).listview("refresh");

}

function parseRSS(url, callback) {
	$
			.ajax({
				url : 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q='
						+ encodeURIComponent(url),
				dataType : 'json',
				success : function(data) {
					callback(data.responseData.feed);
				}
			});
}